package com.zq.sysuser.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.zq.common.logs.SysOperLog;
import com.zq.common.util.Result;
import com.zq.sysuser.dao.entity.User;
import com.zq.sysuser.mapper.SysOperLogMapper;
import com.zq.sysuser.service.IUserService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;

/**
 * @Author 张乔
 * @Date 2025/2/30 13:29
 */
@RestController
@RequestMapping("/sys/logs")
public class Logs {

    @Resource
    private SysOperLogMapper sysOperLogMapper;

    @Resource
    private IUserService userService;

    @PostMapping("/save")
    public Result<String> save(@RequestBody SysOperLog  sysOperLog){

        // 保存日志
        com.zq.sysuser.dao.entity.SysOperLog sysOperLog2 = new com.zq.sysuser.dao.entity.SysOperLog();
        BeanUtils.copyProperties(sysOperLog, sysOperLog2);
        // 获取当前账号 id 的 Account-Session (必须是登录后才能调用)
//        SaSession session = StpUtil.getSession();
//        session.get("id");
        Object loginId = StpUtil.getSession().getLoginId();
        User byId = userService.getById((Serializable) loginId);
        if (byId == null) {
            throw new RuntimeException("用户不存在");
        }
        sysOperLog2.setOperName(byId.getUsername());
        sysOperLogMapper.insert(sysOperLog2);

        return Result.success();



    }


}
